﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Orden;
using Processes.Orden;
using System.Web.Security;
namespace Interfaces.Orden
{
    public partial class Ver : System.Web.UI.Page
    {
        GestorOrden gestorOrden;
        string Nro;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            gestorOrden = new GestorOrden();
            if (!IsPostBack)
            {
                Nro = Request.QueryString["Nro"];
                if (Nro != null)
                {
                        Session["NroReciv"] = Nro;
                    if (Request.QueryString["Fecha"] != null && Request.QueryString["FechaUltimaModificacion"] != null)
                    {
                        bool estaAprobada;
                        try
                        {
                            estaAprobada= gestorOrden.EstaAprobada(Nro);
                            if (!estaAprobada && Session["BuscarMaster"] != null && !Session["BuscarMaster"].ToString().Equals("~/MasterPages/SecretaryMaster.Master"))
                            {
                                ButtonAceptar.Visible = true;

                            }
                        }
                        catch (Exception ex)
                        {
                            
                            throw ex;
                        }
                        
                        DateTime Fecha = DateTime.Parse(Request.QueryString["Fecha"]);
                        DateTime FechaModif = DateTime.Parse(Request.QueryString["FechaUltimaModificacion"]);
                        MultiView1.ActiveViewIndex = 0;
                        Label2.Text += " " + Nro;

                        FechaLabel.Text = Fecha.Date.ToString();
                        FechaModifLabel.Text = FechaModif.ToString();

                        List<Punto> puntos = new List<Punto>();
                        GestorOrden gestor = new GestorOrden();
                        puntos = gestor.puntosDeNro(Nro);
                        if (puntos != null)
                        {
                            foreach (Punto punto in puntos)
                            {
                                string s = calcularTabs(punto.NumeroPunto)+punto.NumeroPunto + " " + punto.NombreDePunto;
                                ListBox1.Items.Add(s);
                            }
                        }
                        else
                        {
                            MultiView1.ActiveViewIndex = 1;
                        }
                    }
                }
                else
                {
                    MultiView1.ActiveViewIndex = 1;
                }
            }
                
            
        }
        private string calcularTabs(string num)
        {
            char[] delimiters = { '.' };
            string[] words = num.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            string tab = "";
            for (int i = 0; i < words.Length; i++)
            {
                tab += "-";
            }
            return tab;
        }
        protected void ButtonAceptar_Click(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null && !Session["BuscarMaster"].ToString().Equals("~/MasterPages/SecretaryMaster.Master"))
            {
                ButtonAceptar.Visible = true;
                string view = "ErrorAprobando.aspx";
                try
                {
                    Nro = (string)Session["NroReciv"];
                    if (gestorOrden.Aprobar(Nro))
                        view = "OrdenAprobada.aspx";
                }
                catch (Exception ex)
                {
                    view = "ErrorAprobando.aspx";
                }
                Response.Redirect(view);
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }

        protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
        {

        }
    }
}